/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.dao.jpa;

import com.wii.elpro.dao.JPAUtil;
import com.wii.elpro.dao.UnitDAOInterface;
import com.wii.elpro.entity.Unit;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author hendro
 */
public class UnitDAOImpl extends DAOGenericImpl implements UnitDAOInterface {

    public boolean unitIsUsed(String nama) throws Exception {
        List<Unit> units = null;
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            units = em.createQuery("SELECT unit FROM Unit unit WHERE unit.nama= :nama").setParameter("nama", nama).getResultList();
        } catch (Exception ex) {
            throw ex;
        }
        if (units.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public Unit getUnit(long id) throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.find(Unit.class, id);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Unit> getUnits() throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.createQuery("SELECT d FROM Unit d").getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }
}
